package cn.fl.risk.service.impl;

import java.util.List;
import java.util.Map;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.risk.api.IRskTelSignLogApiService;
import cn.fl.risk.condition.RskTelSignLogQC;
import cn.fl.risk.dto.RskTelSignLogDTO;
import cn.fl.risk.service.IRskTelSignLogService;
import cn.fl.risk.transform.RskTelSignLogVOTransform;
import cn.fl.risk.vo.RskTelSignLogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 手机号码风险标记记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class RskTelSignLogServiceImpl implements IRskTelSignLogService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(RskTelSignLogServiceImpl.class);
	
	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	@Autowired
	private IRskTelSignLogApiService rskTelSignLogApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public RskTelSignLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		RskTelSignLogDTO dto = this.rskTelSignLogApiService.selectById(id);
		return RskTelSignLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(RskTelSignLogVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//TODO 业务逻辑
		return this.rskTelSignLogApiService.add(RskTelSignLogVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//TODO 业务逻辑
		return this.rskTelSignLogApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(RskTelSignLogVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		//TODO 业务逻辑
		return this.rskTelSignLogApiService.modify(RskTelSignLogVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		//TODO 业务逻辑
		page = this.rskTelSignLogApiService.searchListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			List<RskTelSignLogVO> voList= RskTelSignLogVOTransform.toVOList((List<RskTelSignLogDTO>)page.getResultList());
			this.setDictData(voList);
			page.setResultList(voList);
		}
		return page;
	}

	@Override
	public List<RskTelSignLogVO> searchList(RskTelSignLogQC condition) throws BaseException {
		List<RskTelSignLogVO> voList= RskTelSignLogVOTransform.toVOList(this.rskTelSignLogApiService.searchList(condition));
		this.setDictData(voList);
		return voList;
	}


	/**
	 * 添加数据字典名称
	 * @param voList
	 */
	public void setDictData(List<RskTelSignLogVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/**
		 * 风险标记类型
		 */
		Map<String, String> resTypeMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_NM_SIGN_RES_TYPE);
		for(RskTelSignLogVO vo:voList){
			if (resTypeMap != null && resTypeMap.containsKey(vo.getSignResType())) {
				vo.setSignResTypeNm(resTypeMap.get(vo.getSignResType()));
			}
		}
	}
}
